package com.example.demo.utils;


import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import javax.sql.DataSource;
//import org.apache.commons.dbcp.DriverManagerConnectionFactory;
//import org.apache.commons.dbcp.PoolableConnection;
//import org.apache.commons.dbcp.PoolableConnectionFactory;
//import org.apache.commons.dbcp.PoolingDataSource;
//import org.apache.commons.pool.impl.GenericObjectPool;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
 
public class ConnectionFactory 
{
	private static interface Singleton {
	final ConnectionFactory INSTANCE = new ConnectionFactory();
}
 
//    private final DruidDataSource dataSource;
    private  DruidDataSource dataSource;
 
    private ConnectionFactory() 
    {
    	try 
    	{
           Class.forName("com.mysql.cj.jdbc.Driver");
        } 
        catch (ClassNotFoundException e) 
        {
           e.printStackTrace();
           System.exit(0);
        }
    	Properties properties = new Properties();
		String user = "root";
		String password = "112200";
		String url = "jdbc:mysql://localhost:3306/dfyz_website?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=GMT";
		String driverClassName = "com.mysql.cj.jdbc.Driver";
		properties.put("driverClassName", driverClassName);
		properties.put("url", url);
		properties.put("username", user);
		properties.put("password", password);
		try {
			this.dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

//        Properties properties = new Properties();
//        properties.setProperty("user", "用户名");
//        properties.setProperty("password", "密码"); //or get properties from some configuration file
// 
//        GenericObjectPool<PoolableConnection>pool = new GenericObjectPool<PoolableConnection>();
//        DriverManagerConnectionFactory connectionFactory = newDriverManagerConnectionFactory(
//                "jdbc:mysql://localhost:3306/数据库名称",properties
//        );
//        new PoolableConnectionFactory(connectionFactory, pool, null,"SELECT 1", 3, false, false, Connection.TRANSACTION_READ_COMMITTED
//        );
 
//        this.dataSource = new PoolingDataSource(pool);
     }
 
     public static Connection getDatabaseConnection() throws SQLException 
     {
         return Singleton.INSTANCE.dataSource.getConnection();
     }
}
